//
//  ZPositionChoseVC.m
//  WeClubs
//
//  Created by Zed Link on 3/5/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZPositionChoseVC.h"
#import "ZCommentHeader.h"
#import "ZResourceManager.h"
#import "ZClubPostionModel.h"
NSString * const PositionChoseCellReuseID = @"ZPositionChoseCellReuseID";
NSString * const PositionChoseHeaderReuseID = @"ZPositionChoseHeaderReuseID";

@interface ZPositionChoseVC () <ZMultiChosePanelCellDelegate>

@end

@implementation ZPositionChoseVC

+ (instancetype)zNew
{
    return [self zNewGroup];
}

#pragma mark - About Navigation

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"职位制定";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"完成" andSelector:@selector(finishBtnAction)];

}

- (void)finishBtnAction{
//    
//    NSMutableString *selectedDepartmentIds = [NSMutableString string];
//    NSMutableString *newDepartmentNames = [NSMutableString string];
//    
//    for (ZClubPostionModel *department in self.positionModels) {
//        if (department.job_id == 0 && department.is_selected == YES) {
//            [newDepartmentNames appendFormat:@"%@,", department.job_name];
//        } else if (department.is_selected == YES) {
//            [selectedDepartmentIds appendFormat:@"%ld,", department.job_id];
//        }
//    }
//    
//    selectedDepartmentIds = [[self removeComma:selectedDepartmentIds] mutableCopy];
//    newDepartmentNames = [[self removeComma:newDepartmentNames] mutableCopy];
    
    NSMutableDictionary *dataPara = [@{
                                       @"user_id" : @([ZUserInfo sharedInstance].userId),
                                       @"token" : [ZUserInfo sharedInstance].token,
                                       @"club_id" : self.clubId ,
                                       @"job_auth" : @(self.job_id),
                                       } mutableCopy];
    
//    if (newDepartmentNames != nil && [newDepartmentNames isEqualToString:@""] == NO) {
//        [dataPara setObject:newDepartmentNames forKey:@"new_department"];
//    }
    [HTTPRequest POST:kSet_jobUrl parameter:dataPara success:^(id resposeObject) {
        
    
        
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
     [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSError *error) {
         [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    
    }];
    

    

}

#pragma mark - View Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.contentInset = UIEdgeInsetsMake(16, 0, 0, 0);
    self.tableView.bounces = NO;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZMultiChosePanelCell" bundle:nil] forCellReuseIdentifier:PositionChoseCellReuseID];
    [self.tableView registerClass:[ZCommentHeader class] forHeaderFooterViewReuseIdentifier:PositionChoseHeaderReuseID];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZMultiChosePanelCell *cell = [tableView dequeueReusableCellWithIdentifier:PositionChoseCellReuseID];
    cell.delegate = self;
    cell.itemModels = self.positionModels;
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZCommentHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:PositionChoseHeaderReuseID];
    header.alignment = ZTableHeaderAlignmentCenter;
    header.style = ZTableHeaderStyleSetting;
    [header zSetTitle:@"职位选择"];
    
    return  header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SETTING_STYLE_HEIGHT;
}

#pragma mark - ZMultiChosePanelCellDelegate

- (void)multiChosePanelCell:(ZMultiChosePanelCell *)cell didSelectedItem:(ZMultiChoseState)itemState AtIndex:(NSUInteger)index
{
    ZMultiChoseModel *model = self.positionModels[index];
    model.selected = itemState;
    
    NSString *position = model.itemName;
    if ([self.delegate respondsToSelector:@selector(positionChoseControllerdidSelectPosition:)]) {
        [self.delegate positionChoseControllerdidSelectPosition:position];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)multiChosePanelCell:(ZMultiChosePanelCell *)cell didSelectedPlusItem:(UIButton *)plusItem
{
    Z_WEAK_SELF
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"添加新职位" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        [textField setPlaceholder:@"请输入职位名称"];
    }];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *textField = alert.textFields.firstObject;
        
        ZMultiChoseModel *model = [[ZMultiChoseModel alloc] init];
        model.itemName = textField.text;
        model.selected = @(0);
        [weakSelf.positionModels addObject:model];

        [weakSelf.tableView reloadData];
    }]];
    
    [self.navigationController presentViewController:alert animated:YES completion:nil];
}

- (NSString *)removeComma:(NSString *)content
{
    if (content == nil || content.length == 0) {
        return content;
    }
    
    NSRange lastRange = NSMakeRange(content.length - 1, 1);
    NSString *comma = [content substringWithRange:lastRange];
    NSMutableString *temp = [NSMutableString stringWithString:content];
    if ([comma isEqualToString:@","]) {
        [temp deleteCharactersInRange:lastRange];
    }
    return [NSString stringWithString:temp];
}

@end
